/*
 * Copyright (c) 2015 www.smarthome.com All rights reserved. 本软件源代码版权归----所有,未经许可不得任意复制与传播.
 */
package com.smarthome.base.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.dao.ShEquipmentUserDao;
import com.smarthome.base.dao.base.BaseDao;
import com.smarthome.base.service.ShEquipmentUserService;
import com.smarthome.base.service.base.BaseServiceImpl;
import com.smarthome.domain.bean.ShEquipmentUser;
import com.smarthome.domain.enums.EquipmentCommTypeEnum;
import com.smarthome.domain.vo.api.UserDeviceBean;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
 * 设备与用户管理
 * ShEquipmentCommService 实现类
 * @author KongXiangyu szc
 * @since 2015-09-07
 */
@Service("shEquipmentUserService")
public class ShEquipmentUserServiceImpl extends BaseServiceImpl<ShEquipmentUser, Long> implements ShEquipmentUserService {

    @Resource
    private ShEquipmentUserDao shEquipmentUserDao;

    public BaseDao<ShEquipmentUser, Long> getDao() {
        return shEquipmentUserDao;
    }

    /**
	 * 查询供应商信息列表--分页
	 * @param deviceSupplier
	 * @param page
	 * @return
	 */
    @Override
    public DataGrid queryShEquipmentUserPage(ShEquipmentUser shEquipmentUser, PageHelper page) {
		DataGrid dataGrid = new DataGrid();
		
		//ShEquipmentUser queryShEquipmentUser = new ShEquipmentUser();
        if (!StringUtils.isBlank(page.getSort())) {
        	shEquipmentUser.setOrderField(page.getSort());
        }
        if (!StringUtils.isBlank(page.getOrder())) {
        	shEquipmentUser.setOrderFieldType(page.getOrder());
        }
        if (!StringUtils.isBlank(shEquipmentUser.getBingType())) {		//绑定类型
        	shEquipmentUser.setBingType(shEquipmentUser.getBingType());
        }
        /* if (null != queryShEquipmentInfo.getDeviceCode()) {					//联系人名称
        	queryShEquipmentInfo.addQueryData("deviceCode", queryShEquipmentInfo.getDeviceCode());
        }
        if (StringUtils.isNotBlank(queryShEquipmentInfo.getDeviceCcNum())) {	//联系人电话
        	queryShEquipmentInfo.addQueryData("deviceCcNum", queryShEquipmentInfo.getDeviceCcNum());
        }
        if (null != queryShEquipmentInfo.getDeviceTypeId()) {				//联系人地址
        	queryShEquipmentInfo.addQueryData("deviceTypeId", queryShEquipmentInfo.getDeviceTypeId());
        }
        if (null != queryShEquipmentInfo.getDeviceSupplierId()) {					//合作时间
        	queryShEquipmentInfo.addQueryData("deviceSupplierId", queryShEquipmentInfo.getDeviceSupplierId());
        }
        if (null != queryShEquipmentInfo.getDeviceState()) {						//创建时间
        	queryShEquipmentInfo.addQueryData("deviceState", queryShEquipmentInfo.getDeviceState());
        }*/
        	
        //
        long totalCount = shEquipmentUserDao.queryShEquipmentUserCount(shEquipmentUser);
        dataGrid.setTotal(totalCount);
        int startIndex = (page.getPage() - 1) * page.getRows();
        int endIndex = page.getPage() * page.getRows();
        if (totalCount > 0 && totalCount > startIndex) {
        	shEquipmentUser.setStartIndex(startIndex);
        	shEquipmentUser.setEndIndex(endIndex);
            List<ShEquipmentUser> shEquipmentUserList = shEquipmentUserDao.queryShEquipmentUserList(shEquipmentUser);
            if(shEquipmentUserList!=null && shEquipmentUserList.size()>0){
            	for(ShEquipmentUser equipmentUser :shEquipmentUserList){
            		if(equipmentUser!=null && equipmentUser.getCommType()!=null){
            			equipmentUser.setCommTypeName(EquipmentCommTypeEnum.getNameById(equipmentUser.getCommType().toString()));
            		}else{
            			equipmentUser.setCommTypeName("");
            		}
                }
            }
            dataGrid.setRows(shEquipmentUserList);
        }
        return dataGrid;
	}
    
    /**
	 * 得到用户的相关终端设备
	 * @param 
	 * @return
	 */
    @Override
	public List<UserDeviceBean> queryUserDeviceBeanList(java.util.HashMap params){
		return shEquipmentUserDao.queryUserDeviceListDao(params);
	}
	
}